** estimate effects by experience/learning 

***Inputs: 
* $Data/GameUserWPdaily_norm.dta
* $Data/maxmindirectionsmonth_zip260.dta


***Outputs: 
* $Results/bylearning
* $Results/learning_pm.png
* $Results/pvals_learning.dta
* $Results/learning_pm.png
* $Results/learning_pmgamefe.png
* $Results/playscore.tex
* $Results/learning_obs.png
* $Results/playobs.tex

capture eststo clear
	
	use $Data/GameUserWPdaily_norm.dta, clear
	
	gen month = month(date)
	
	gen zip2 = int( zip/10)
	
	merge m:1 zip2 month using $Data/maxmindirectionsmonth_zip260.dta
	
	keep if _merge ==3
	
	
	gen monthyear = mofd(date)

	gen highpm =  weighted_dailypm > 25 & weighted_dailypm<. 
		

	egen cut_temp = cut(weighted_temperature), at(-40(10)40)

	egen cut_winddirection = cut(median_closestwind), at(0(60)360)
	replace cut_winddirection = 0 if median_closestwind ==360	
	
	gen maxzipwind = (cut_winddirection ==maxdirection )
	replace maxzipwind = . if maxdirection ==.
		
		

	egen cut_play = cut(nth_play), at(0(5)20)

	egen cut_age = cut(age), at(15(10)85)

	by anon_id gamenum, sort: egen maxplay = max(nth_play)
	
	gen above5play = nth_play >= 5 & nth_play<.

	
	
	
ivreghdfe norm_score (c.highpm#i.above5play =c.maxzipwind#i.above5play) i.above5play weighted_wind_speed weighted_precipitation  if maxplay >=  20  & maxplay<., absorb(anon_id monthyear cut_temp  ) cluster(anon_id monthyear)
	

	
	eststo above5inthighplay 
	
	estimates save $Results/bylearning, replace
	
	regsave 0b.above5play#c.highpm 1.above5play#c.highpm 0b.above5play 1.above5play using $Results/pvals_learning.dta, pval 

	
	
	
	estimates restore above5inthighplay 
	
	

	
	test 0.above5play#c.highpm + 0.above5play = 1.above5play#c.highpm + 1.above5play 
	
	test 0.above5play#c.highpm  = 1.above5play#c.highpm 
	
	test 1.above5play#c.highpm - 0.above5play#c.highpm =  1.above5play  - 0.above5play 
	
	
	
	lincom 1.above5play#c.highpm + 1.above5play
	
	matrix C = r(estimate),  r(lb), r(ub) 
	
		
	coefplot (above5inthighplay , keep(0.above5play 1.above5play) ciopts(recast(rcap) lcolor(red))  mcolor(red) msymbol(D) msize(small)) ///
	 (above5inthighplay , keep(0.above5play#c.highpm ) ciopts(recast(rcap) lcolor(blue))  mcolor(blue) msymbol(D) msize(small)) ///	
	(matrix(C[,1]), ci((C[,2] C[,3])) ciopts(recast(rcap) lcolor(blue))  mcolor(blue) msymbol(D) msize(small)) ///	
	, omitted baselevels drop(weighted*) vertical nooffsets levels(95) yline(0)   legend(off)  ///
	coeflabels(0.above5play = "First 5 plays" ///
	1.above5play = "> 5 plays" ///
	0.above5play#c.highpm  = "First 5 plays" ///
	r1  = "> 5 plays" ) groups(0.above5play 1.above5play = "{bf:Daily PM2.5<= 25}" 0.above5play#c.highpm r1  = "{bf:Daily PM2.5>25}" )
	
	
	graph export $Results/learning_pm.png , replace
	


	
	
	
	
	
	
ivreghdfe norm_score (c.highpm#i.above5play =c.maxzipwind#i.above5play) i.above5play weighted_wind_speed weighted_precipitation  if maxplay >=  20  & maxplay<., absorb(anon_id monthyear cut_temp gamenum ) cluster(anon_id monthyear)
	

	
	eststo above5inthighplay 
	
	
	
	
	estimates restore above5inthighplay 
	
	

	
	
	test 0.above5play#c.highpm + 0.above5play = 1.above5play#c.highpm + 1.above5play // highly sig (testing learning when polluted) 
	
	
	test 0.above5play#c.highpm  = 1.above5play#c.highpm // sig diff at 10% (difference in learning)  
	
	
	
	
	
	
	lincom 1.above5play#c.highpm + 1.above5play
	
	matrix C = r(estimate),  r(lb), r(ub) 
	
	coefplot (above5inthighplay , keep(0.above5play 1.above5play) ciopts(recast(rcap) lcolor(red))  mcolor(red) msymbol(D) msize(small)) ///
	 (above5inthighplay , keep(0.above5play#c.highpm ) ciopts(recast(rcap) lcolor(blue))  mcolor(blue) msymbol(D) msize(small)) ///	
	(matrix(C[,1]), ci((C[,2] C[,3])) ciopts(recast(rcap) lcolor(blue))  mcolor(blue) msymbol(D) msize(small)) ///	
	, omitted baselevels drop(weighted*) vertical nooffsets levels(95) yline(0)   legend(off)  ///
	coeflabels(0.above5play = "First 5 plays" ///
	1.above5play = "> 5 plays" ///
	0.above5play#c.highpm  = "First 5 plays" ///
	r1  = "> 5 plays" ) groups(0.above5play 1.above5play = "{bf:Daily PM2.5<= 25}" 0.above5play#c.highpm r1  = "{bf:Daily PM2.5>25}" )
	
	
	graph export $Results/learning_pmgamefe.png , replace
	
	
	
	
	
	
	gen ones = 1
	
		gen playbins = 1 if nth_play <=5

	
	forval p = 2(1)10 {
	
	local d = (`p'-1) *5
	local n = `p'*5
	
	replace playbins =`p' if nth_play > `d' & nth_play<=`n'
	
	
	
	}
	
	
	label define plays 1 "5" 2 "10" 3 "15" 4 "20" 5 "25" 6 "30" 7 "35" 8 "40" 9 "45" 10 "50"
	label values playbins plays
	
	label variable norm_score "Standardized Score"
	
	reg norm_score i.playbins if maxzipwind<. 

	eststo cutplay
	
	
	
		esttab cutplay using $Results/playscore.tex, ci(3) b(3)  replace label 

	
	
	collapse (sum) Obs = ones , by(playbins)

	label variable Obs "Observations (000s)"

	replace Obs = Obs/1000
	


	
	coefplot (cutplay, keep(1.playbins) ciopts(recast(rcap) lcolor(red))   mcolor(red) ) ///
	(cutplay, keep(2.playbins) ciopts(recast(rcap) lcolor(orange)) mcolor(orange)) ///
	(cutplay, keep(3.playbins) ciopts(recast(rcap)  lcolor(gold)) mcolor(gold)) ///
	(cutplay, keep(4.playbins) ciopts(recast(rcap) lcolor(green)) mcolor(green) ) ///
	(cutplay, keep(5.playbins) ciopts(recast(rcap)   lcolor(blue)) mcolor(blue)) ///
	(cutplay, keep(6.playbins) ciopts(recast(rcap) lcolor("111 000 255")) mcolor("111 000 255")) ///
	(cutplay, keep(7.playbins) ciopts(recast(rcap) lcolor(red))   mcolor(red) ) ///
	(cutplay, keep(8.playbins) ciopts(recast(rcap) lcolor(orange)) mcolor(orange)) ///
	(cutplay, keep(9.playbins) ciopts(recast(rcap)  lcolor(gold)) mcolor(gold)) ///
	(cutplay, keep(10.playbins) ciopts(recast(rcap) lcolor(green)) mcolor(green) ) ///
	,  vertical levels(95) yline(0) yaxis(2)  ytitle("Difference in score relative to 5 plays", axis(2)) nooffsets legend(off) xlabel(, angle(60))  msymbol(D) omitted baselevels 
	
		addplot: bar Obs playbins if playbins ==1, yaxis(1) bcolor(red%20) 
		addplot: bar Obs playbins if playbins ==2, yaxis(1) bcolor(orange%20)  
		addplot: bar Obs playbins if playbins ==3, yaxis(1) bcolor(gold%20) 
		addplot: bar Obs playbins if playbins ==4, yaxis(1) bcolor(green%20) 
		addplot: bar Obs playbins if playbins ==5, yaxis(1) bcolor(blue%20)  
		addplot: bar Obs playbins if playbins ==6, yaxis(1) bcolor("111 000 255 %20")  
		addplot: bar Obs playbins if playbins ==7, yaxis(1) bcolor(red%20) 
		addplot: bar Obs playbins if playbins ==8, yaxis(1) bcolor(orange%20)  
		addplot: bar Obs playbins if playbins ==9, yaxis(1) bcolor(gold%20) 
		addplot: bar Obs playbins if playbins ==10, yaxis(1) bcolor(green%20) ytitle(Observations (000s), axis(1))
		
		
		
		
		
		graph export $Results/learning_obs.png, replace
		
		
		
		
		
		
		estpost tabstat Obs , by(playbins) 	statistics(mean ) columns(statistics) listwise

 
		
		esttab . using $Results/playobs.tex, main(mean) nostar unstack noobs nonote label mtitle("1000 Observations") replace
		
		
		
		
		
		
		
